<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-user-edit" th:object="${yhq}">
			<input name="yhqId"  type="hidden"  th:field="*{yhqId}" />
			<input name="yhqType"  type="hidden"  th:field="*{yhqType}" id="treeId"/>

			<div class="form-group">
				<label class="col-sm-3 control-label">优惠类型：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="yhqTypeName" onclick="selectTypeTree()" readonly="true" id="treeName" th:field="*{yhqTypeName}">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">有效期：</label>
				<div class="col-sm-8">
					<li class="select-time">
						<label>开始时间： </label>
						<input type="text" th:value="${stt}" class="time-input" id="startTime" placeholder="开始时间" name="startTime"/>
						<label>结束时间： </label>
						<input type="text" th:value="${edt}" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
					</li>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">优惠券数量：</label>
				<div class="col-sm-8">
					<input id="yhqAmount" name="yhqAmount" th:field="*{yhqAmount}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">折扣额：</label>
				<div class="col-sm-8">
					<input id="yhqDiscount" name="yhqDiscount" th:field="*{yhqDiscount}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">时长卡时长：</label>
				<div class="col-sm-8">
					<input id="yhqHours" name="yhqHours" th:field="*{yhqHours}" class="form-control" type="text">
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
						<input type="checkbox" th:checked="${yhq.yhqState == 1 ? true : false}" class="onoffswitch-checkbox" id="yhqState" name="yhqState">
						<label class="onoffswitch-label" for="yhqState">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>


		</form>
	</div>
	<div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/select/select2.js}"></script>
	<script>
        $("#form-user-edit").validate({
        	rules:{
/*        		userName:{
        			required:true,
        		},*/
        		typeName:{
        			required:true,
        		},
				startTime:{
					required:true,
				},
				endTime:{
					required:true,
				},

			},
        	submitHandler:function(form){
        		edit();
        	}

        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	        	edit();
	        }
	    }

        function edit() {
        	var yhqId = $("input[name='yhqId']").val();
        	var yhqType = $("input[name='yhqType']").val();

        	var startTime = $("input[name='startTime']").val();
        	var endTime = $("input[name='endTime']").val();

        	var yhqAmount = $("input[name='yhqAmount']").val();
        	var yhqDiscount = $("input[name='yhqDiscount']").val();
        	var yhqHours = $("input[name='yhqHours']").val();
        	// var yhqHours = $("#sex option:selected").val();
			var yhqState = $("input[name='yhqState']").is(':checked') == true ? 1 : 0;
        	// var roleIds = $.form.selectCheckeds("role");
        	// var postIds = $.form.selectSelects("post");
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "gxgw_modules/yhq/edit",
        		data : {
        			"yhqId": yhqId,
        			"yhqType": yhqType,
					"startTime": startTime,
					"endTime": endTime,
        			"yhqAmount": yhqAmount,
        			"yhqDiscount": yhqDiscount,
        			"yhqHours": yhqHours,
					"yhqState": yhqState,
        			// "status": status,
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.successCallback(data);
        		}
        	});
        }

        /*优惠类型-修改-选择类型树*/
        function selectTypeTree() {
        	var yhqType = $.common.isEmpty($("#treeId").val()) ? "1" : $("#treeId").val();
            var url = ctx + "gxgw_modules/yhqType/selectTypeTree/" + yhqType;
		    var options = {
				title: '选择类型',
				width: "380",
				url: url,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
    			var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			}
		}
    </script>
</body>
</html>
